package com.sunlg.smarthome.contract.presenter;


import android.view.ViewGroup;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.base.C;
import com.sunlg.smarthome.contract.PumpCtrlContract;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

/**
 * Created by sunlg on 2017/5/27.
 */

public class PumpCtrlPresenter implements PumpCtrlContract.Presenter {

    private PumpCtrlContract.View mView;
    private SHServiceWrapper mSHServiceWrapper;
    private PreferHelper mPreferHelper;

    public PumpCtrlPresenter() {
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
        mPreferHelper = new PreferHelper(App.getInstance(), "gateway");
    }

    @Override
    public void startPump(int pumpId, int level) {
        mSHServiceWrapper.startPump(pumpId, level, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.startPumpSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void stopPump(int pumpId) {
        mSHServiceWrapper.stopPump(pumpId, new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if(data.getCode() == C.ActionStatus.SUCCESS){
                    mView.stopPumpSuccess();
                }else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
                mView.showError(e.getMessage());
            }
        });
    }

    @Override
    public void attachView(PumpCtrlContract.View view) {
        mView = view;
    }

    @Override
    public void dettachView() {
        mView = null;
    }
}
